<?php
/*
 * Created on 2012-3-30
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class User_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('session');
 	}

/*
 * 检测用户登录
 */
	public function model_checklogin()
	{
		$username = $this->input->post('username');
		$pwd = $this->input->post('userpwd');
		$sql = "select * from tj_user where user_name='".$username."' and password ='".md5($pwd)."' limit 1";
		$query = $this->db->query($sql);
		$result = $query->row_array();
		if(!empty($result))
		{
			/*
			 * 更新信息
			 */
			 $new_integral = intval($result['integral'])+1;
			 $data = array('last_ip'=>$this->input->ip_address(),
			 			   'last_login'=>time(),
			 			   'integral'=>$new_integral
			 				);
			 $this->db->update('tj_user',$data,array('uid'=>$result['uid']));
			 $result['integral'] = $new_integral;
			 $result['add_date'] = date('Y-m-d',$result['add_time']);//注册时间
			 $this->session->set_userdata($result);
			return true;
		}else{
			return false;
		}
	}

	/*
	 * 用户登出
	 */
	 public function model_loginout()
	 {
		$this->session->sess_destroy();
	 }

	 /*
	  * 修改密码
	  *
	  */
	 public function model_changePwd()
	 {
		$uid = $this->session->userdata('uid');
		$oldPwd = $this->input->post('oldPwd');
		$newPwd = $this->input->post('newPwd');
		$sql = "select * from tj_user where uid=".$uid;
		$query = $this->db->query($sql);
		$result = $query->row_array();
		if(!empty($result))
		{
			 $this->db->update('tj_user', array('password'=>md5($newPwd)), array('uid'=>$result['uid']));
			 return true;
		}else{
			return false;
		}
	 }
	/**
	 * 新用户注册
	 */
	 public function model_newRegister()
	 {

		 $register = array();
		 $register['user_name'] = trim($this->input->post('username'));
		 $register['password'] = md5(trim($this->input->post('userpwd')));
		 $register['add_time'] = time();
		 $register['companyName'] = trim($this->input->post('companyName'));
		 $register['contactor'] = $this->input->post('contactor');
		 $register['tel'] = $this->input->post('tel');
		 $register['last_login'] = time();
		 $register['last_ip'] = $this->input->ip_address();
		 $register['email'] = $this->input->post('email');
		 $register['province_id'] = $this->input->post('province');
		 $register['province'] = trim($this->input->post('hid_province'));
		 $register['city'] = $this->input->post('city');
		 $register['address'] = $this->input->post('address');
		 $register['remark'] = $this->input->post('remark');

		 $this->db->insert('tj_user', $register);
		 $register['uid'] = $this->db->insert_id();
		 $this->session->set_userdata($register);
		 return true;
	 }
 }
?>
